package com.ebo.upload;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.ebo.agora.openvcall.IBaseActivity;

import java.util.ArrayList;
import java.util.List;

import io.agora.openvcall.R;
import utils.FilePath;
import utils.IToast;
import utils.Lg;

/**
 * Created by admin on 2017/6/6.
 */

public class UploadListActivity extends IBaseActivity implements View.OnClickListener {

    public static List<MultipartUpload> list_Upload = new ArrayList<MultipartUpload>();
    UploadAdaper uploadAdaper;
    ListView lv_upload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_list);
        initViews();
//        registerBroadCast();
    }

    RelativeLayout actionbar_back;
    ImageView iv_SelectFile;

    void initViews() {
        actionbar_back = (RelativeLayout) findViewById(R.id.actionbar_back);
        actionbar_back.setOnClickListener(this);
        iv_SelectFile = (ImageView) findViewById(R.id.iv_SelectFile);
        iv_SelectFile.setOnClickListener(this);
        uploadAdaper = new UploadAdaper(this, list_Upload);
        lv_upload = (ListView) findViewById(R.id.lv_upload);
//        lv_upload.setAdapter(uploadAdaper);
    }

    //按键====================================================================================
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.actionbar_back:
                onClickBack(v);
                break;
            case R.id.iv_SelectFile:
                onClickSelectFile(null);
                break;
        }
    }

    //选择要上传的文件
    public void onClickSelectFile(View v) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                startActivityForResult(intent, 100);
            }
        }).start();
    }


    //返回
    public void onClickBack(View v) {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onClickBack(null);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    //逻辑===============================================================================
    MultipartUpload task_Upload;

    //    MultipartUpload.progressListener listener;
    //选择文件后
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            Lg.d("data==null");
            return;
        }
        Uri Uri_UploadFile = data.getData();
        final String uploadFilePath = FilePath.getFileAbsolutePath(this, Uri_UploadFile);
        switch (requestCode) {
            case 100:

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            task_Upload = new MultipartUpload(uploadFilePath);
                            task_Upload.multipartUpload();
//                            uploadAdaper.notifyDataSetChanged();

                            list_Upload.add(task_Upload);
                            lv_upload.setAdapter(uploadAdaper);
                            UploadListActivity.this.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    uploadAdaper.notifyDataSetChanged();
                                    // toggleView(true);
                                }
                            });

                        } catch (Exception e) {
                            IToast.show("该文件上传失败！");
//                            Log.e("upload error! " + e.toString());
                        }
                    }
                }).start();


                break;
        }
    }


}
